<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: best_sellers.php 140 2013-09-22 07:44:23Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 140 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-22 07:44:23 +0000 (Sun, 22 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

$BestsellerDays = 30;
$CatDependant = true;

if(!defined('BESTSELLER_DAYS'))
	define('BESTSELLER_DAYS',$BestsellerDays);
		
$box_smarty = new Smarty();
$box_content = '';

$box_smarty->assign('language', $_SESSION['language']);

if(!CacheCheck() || !FORCE_CACHE) {
 	$cache=false;
	$box_smarty->caching = false;
} else {
	$cache=true;
	$box_smarty->caching = true;
	$box_smarty->cache_lifetime = CACHE_LIFETIME;
	$box_smarty->cache_modified_check = CACHE_CHECK;
	if($CatDependant)
		$Cat = 'box_bestseller_'.$current_category_id.'_';
	$cache_id = $Cat.$_SESSION['language'].'_stat-id-'.$_SESSION['customers_status']['customers_status_id'].'_'.$current_category_id;
}

if(!$box_smarty->isCached(CURRENT_TEMPLATE.'/boxes/box_best_sellers.html', $cache_id) || !$cache) {
	$box_smarty->assign('tpl_path', 'templates/'.CURRENT_TEMPLATE.'/');
	$box_smarty->assign('html_tpl_path', CURRENT_TEMPLATE.'/html');

	if ($_SESSION['customers_status']['customers_fsk18_display'] == '0')
		$fsk_lock = ' AND p.products_fsk18 !=1';

	if(GROUP_CHECK == 'true') {
		$group_check = " AND p.group_permission_".$_SESSION['customers_status']['customers_status_id']." = 1 ";
		$group_check .= " AND c.group_permission_".$_SESSION['customers_status']['customers_status_id']." = 1 ";
	}
	
	$list_data = get_list_data('best_sellers');
	
	if(isset($current_category_id) && $current_category_id > 0 && $CatDependant) {
		$best_sellers = $db->db_query_limit("SELECT 
													op.products_id, 
													op.products_quantity 
												FROM
													".TABLE_ORDERS." o,
													".TABLE_ORDERS_PRODUCTS." op, 
													".TABLE_PRODUCTS." p,
													".TABLE_PRODUCTS_TO_CATEGORIES." p2c, 
													".TABLE_CATEGORIES." c 
												WHERE
													op.orders_id = o.orders_id 
												AND	
													TO_DAYS(NOW()) - TO_DAYS(o.date_purchased) <= ".(int)BESTSELLER_DAYS."
												AND	
													p.products_id = op.products_id 
												AND	
													p.products_status = '1' 
												AND 
													c.categories_status = '1' 
												AND 
													p.products_id = p2c.products_id 
												AND 
													p2c.categories_id = c.categories_id 
												AND	
													'".$current_category_id."' IN (c.categories_id, c.parent_id) 
													".$fsk_lock.$group_check, $list_data['box_count'], true);
	} else {
		$best_sellers = $db->db_query_limit("SELECT
													op.products_id, 
													op.products_quantity 
												FROM
													".TABLE_ORDERS." o,	
													".TABLE_ORDERS_PRODUCTS." op, 
													".TABLE_PRODUCTS_TO_CATEGORIES." p2c, 
													".TABLE_CATEGORIES." c, 
													".TABLE_PRODUCTS." p
												WHERE
													op.orders_id = o.orders_id  
												AND	
													TO_DAYS(NOW()) - TO_DAYS(o.date_purchased) <= ".(int)BESTSELLER_DAYS."
												AND	
													p.products_id = op.products_id 
												AND	
													p.products_status = '1' 
												AND	
													p2c.categories_id = c.categories_id 
												AND	
													c.categories_status = '1' 
												AND 
													p.products_id = p2c.products_id 
													".$fsk_lock.$group_check, $list_data['box_count'], true);
	}

	if($best_sellers->_numOfRows >= MIN_DISPLAY_BESTSELLERS) {
		while(!$best_sellers->EOF) {
			$prod_ids[$best_sellers->fields['products_id']] += $best_sellers->fields['products_quantity'];
			$best_sellers->MoveNext();
		}
		
		if($prod_ids) {
			require_once(DIR_FS_INC.'inc.row_number_format.php');
			arsort($prod_ids);
			$Keys = array_keys($prod_ids);
			$Count = 0;
			$box_bestseller_content = array();
			while($Count < $list_data['box_count']) {
				if($prod_ids[$Keys[$Count]]) { $row++;
					unset($bestsellerData);
					$bestseller = new product($Keys[$Count]);
					$bestsellerData = $bestseller->buildDataArray($bestseller->data, 'thumbnail', $list_data, $rows, 172);
					$bestsellerData['COUNT'] = row_number_format($Count+1);
					if($_SESSION['customers_status']['customers_status_id'] == 0) 
						$bestsellerData['PRODUCTS_NAME'] .= ' ('.$prod_ids[$Keys[$Count]].'x)';
					$box_bestseller_content[] = $bestsellerData;
				}
				$Count++;
			}
		}

		$box_smarty->assign('box_bestseller_content', $box_bestseller_content);
		if($list_data['box_effect'] != 'none') {
			$get[] = 'bs='.$list_data['box_effect'];
			$get[] = 'bsd='.$list_data['effect_direction'];
		}

		if(!$cache) {
		 	if ($box_bestseller_content!='')
				$box_content = $box_smarty->fetch(CURRENT_TEMPLATE.'/boxes/box_best_sellers.html');
		} else
			$box_content = $box_smarty->fetch(CURRENT_TEMPLATE.'/boxes/box_best_sellers.html', $cache_id);
	}
}